package com.sykj.cloud.system.controller;

import java.util.Iterator;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.sykj.cloud.common.core.utils.StringUtils;
import com.sykj.cloud.system.api.domain.OrgDept;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.sykj.cloud.common.log.annotation.Log;
import com.sykj.cloud.common.log.enums.LogUserType;
import com.sykj.cloud.common.security.annotation.PreAuthorize;

import com.sykj.cloud.system.service.IOrgDeptService;
import com.sykj.cloud.common.core.web.controller.BaseController;
import com.sykj.cloud.common.core.web.domain.AjaxResult;
import com.sykj.cloud.common.core.utils.poi.ExcelUtil;

/**
 * 组织部门Controller
 *
 * @author mr.will
 * @date 2021-03-15
 */
@RestController
@RequestMapping("/org/dept")
public class OrgDeptController extends BaseController
{
    @Autowired
    private IOrgDeptService orgDeptService;


    /**
     * 新增组织部门
     */
    @PreAuthorize(hasPermi = "system:dept:add")
    @Log(title = "组织部门", logUserType = LogUserType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OrgDept orgDept)
    {
        return toAjax(orgDeptService.insertOrgDept(orgDept));
    }

    /**
     * 修改组织部门
     */
    @PreAuthorize(hasPermi = "system:dept:edit")
    @Log(title = "组织部门", logUserType = LogUserType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OrgDept orgDept)
    {
        return toAjax(orgDeptService.updateOrgDept(orgDept));
    }

    /**
     * 删除组织部门
     */
    @PreAuthorize(hasPermi = "system:dept:remove")
    @Log(title = "组织部门", logUserType = LogUserType.DELETE)
	@DeleteMapping("/{guids}")
    public AjaxResult remove(@PathVariable String[] guids)
    {
        return toAjax(orgDeptService.deleteOrgDeptByIds(guids));
    }


    /**
     * 获取组织部门详细信息
     */
    @PreAuthorize(hasPermi = "system:dept:query")
    @GetMapping(value = "/{guid}")
    public AjaxResult getInfo(@PathVariable("guid") String guid)
    {
        return AjaxResult.success(orgDeptService.getOrgDeptById(guid));
    }

    /**
     * 导出组织部门列表
     */
    @PreAuthorize(hasPermi = "system:dept:export")
    @Log(title = "组织部门", logUserType = LogUserType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OrgDept orgDept) throws IOException
    {
        List<OrgDept> list = orgDeptService.listOrgDepts(orgDept);
        ExcelUtil<OrgDept> util = new ExcelUtil<OrgDept>(OrgDept.class);
        util.exportExcel(response, list, "dept");
    }


    /**
     * 查询组织部门列表
     */
    @PreAuthorize(hasPermi = "system:dept:list")
    @GetMapping("/list")
    public AjaxResult list(OrgDept orgDept)
    {
        List<OrgDept> list = orgDeptService.listOrgDepts(orgDept);
        return AjaxResult.success(list);
    }


    /**
     * 查询部门列表（排除节点）
     */
    @PreAuthorize(hasPermi = "system:dept:list")
    @GetMapping("/list/exclude/{guid}")
    public AjaxResult excludeChild(@PathVariable(value = "guid", required = false) String deptId)
    {
        List<OrgDept> depts = orgDeptService.listOrgDepts(new OrgDept());
        Iterator<OrgDept> it = depts.iterator();
        while (it.hasNext())
        {
            OrgDept d = (OrgDept) it.next();
            if (d.getGuid() .equals(deptId)
                    || ArrayUtils.contains(StringUtils.split(d.getParentId(), ","), deptId + ""))
            {
                it.remove();
            }
        }
        return AjaxResult.success(depts);
    }

    /**
     * 获取部门下拉树列表
     */
    @GetMapping("/treeselect")
    public AjaxResult treeselect(OrgDept dept)
    {
        List<OrgDept> depts = orgDeptService.listOrgDepts(dept);
        return AjaxResult.success(orgDeptService.buildDeptTreeSelect(depts));
    }

    /**
     * 加载对应角色部门列表树
     */
    @GetMapping(value = "/roleDeptTreeselect/{roleId}")
    public AjaxResult roleDeptTreeselect(@PathVariable("roleId") String roleId)
    {
        List<OrgDept> depts = orgDeptService.listOrgDepts(new OrgDept());
        AjaxResult ajax = AjaxResult.success();
        ajax.put("checkedKeys", orgDeptService.listDeptsByRoleId(roleId));
        ajax.put("depts", orgDeptService.buildDeptTreeSelect(depts));
        return ajax;
    }


}
